//
//  ByShopDetailProductDescCell.m
//  hutouben_ios
//
//  Created by pro on 16/11/30.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByCellProductDeatilEvaluate.h"

#pragma mark - layout
@implementation ByCellLayoutProductDeatilEvaluate

- (void)setModel:(ByModelProductDetail *)model {
    
    _model = model;
    
    CGFloat x, y, width, height;
    
    // 标题
    x               = 10;
    y               = 10;
    width           = SCREEN_WIDTH - x * 2;
    height          = 20;
    _titleFrame     = CGRectMake(x, y, width, height);
    
    // 标签
    x               = 0;
    y               = CGRectGetMaxY(_titleFrame) + 10;
    width           = SCREEN_WIDTH;
    height          = 25;
    _tagFrame       = CGRectMake(x, y, width, height);
    
    // 用户头像
    x               = 10;
    y               = CGRectGetMaxY(_tagFrame) + 10;
    width           = 20;
    height          = width;
    _userIconFrame  = CGRectMake(x, y, width, height);
    
    // 用户昵称
    x               = CGRectGetMaxX(_userIconFrame) + 10;
    y               = CGRectGetMinY(_userIconFrame);
    width           = SCREEN_WIDTH - 10 - x;
    height          = CGRectGetHeight(_userIconFrame);
    _userNameFrame  = CGRectMake(x, y, width, height);
    
    // 评价内容
    x               = 10;
    y               = CGRectGetMaxY(_userNameFrame) + 10;
    width           = SCREEN_WIDTH - x * 2;
    height          = [model.evaluate_content getRectOfStringWithSize:CGSizeMake(width, 10000) font:12].size.height;
    _contentFrame   = CGRectMake(x, y, width, height);
    
    // 规格
    x               = 10;
    y               = CGRectGetMaxY(_contentFrame) + 5;
    width           = SCREEN_WIDTH - x * 2;
    height          = 15;
    _skuFrame       = CGRectMake(x, y, width, height);
    
    
    // 查看更多按钮
    NSString *text  = @"查看更多评价";
    width           = [text getSizeOfString:16].width + 20;
    height          = 25;
    x               = (SCREEN_WIDTH - width) / 2;
    y               = CGRectGetMaxY(_skuFrame) + 15;
    _moreBtnFrame   = CGRectMake(x, y, width, height);
    
    // cell
    x               = 0;
    y               = 0;
    width           = SCREEN_WIDTH;
    height          = CGRectGetMaxY(_moreBtnFrame) + 15;
    _cellFrame      = CGRectMake(x, y, width, height);
    
}

@end


@implementation ByCellProductDeatilEvaluate
{
    UILabel     *_titleLabel;               // 标题
    UIView      *_tagView;                  // 标签
    UIImageView *_iconImgView;              // 头像
    UILabel     *_usernameLabel;            // 用户名
    UILabel     *_contentLabel;             // 内容
    UILabel     *_skuLabel;                 // 规格
    UIButton    *_moreBtn;                  // 按钮
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    // 标题
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.adjustsFontSizeToFitWidth = YES;
    _titleLabel.font = FONT(14);
    _titleLabel.text = @"商品评价";
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_titleLabel];
    
    // 标签
    _tagView = [[UIView alloc] init];
    [self.contentView addSubview:_tagView];
    
    // 头像
    _iconImgView = [[UIImageView alloc] init];
    [self.contentView addSubview:_iconImgView];
    
    // 用户名
    _usernameLabel = [[UILabel alloc] init];
    _usernameLabel.adjustsFontSizeToFitWidth = YES;
    _usernameLabel.font = FONT(14);
    _usernameLabel.textColor = [UIColor blackColor];
    _usernameLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_usernameLabel];
    
    // 评价内容
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.adjustsFontSizeToFitWidth = YES;
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = FONT(12);
    _contentLabel.textColor = [UIColor blackColor];
    _contentLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_contentLabel];
    
    // 规格
    _skuLabel = [[UILabel alloc] init];
    _skuLabel.adjustsFontSizeToFitWidth = YES;
    _skuLabel.font = FONT(12);
    _skuLabel.textColor = [UIColor lightGrayColor];
    _skuLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_skuLabel];
    
    // 按钮
    _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_moreBtn setTitle:@"查看更多评价" forState:UIControlStateNormal];
    [_moreBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [_moreBtn addTarget:self action:@selector(moreClick) forControlEvents:UIControlEventTouchUpInside];
    _moreBtn.titleLabel.font = FONT(14);
    _moreBtn.layer.cornerRadius = 5;
    _moreBtn.layer.borderColor = [UIColor orangeColor].CGColor;
    _moreBtn.layer.borderWidth = 1;
    [self.contentView addSubview:_moreBtn];
}
- (void)setLayout:(ByCellLayoutProductDeatilEvaluate *)layout {
    
    _layout = layout;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    ByModelProductDetail *model     = _layout.model;
    
    // 设置布局
    _titleLabel.frame               = _layout.titleFrame;
    _tagView.frame                  = _layout.tagFrame;
    _iconImgView.frame              = _layout.userIconFrame;
    _usernameLabel.frame            = _layout.userNameFrame;
    _contentLabel.frame             = _layout.contentFrame;
    _skuLabel.frame                 = _layout.skuFrame;
    _moreBtn.frame                  = _layout.moreBtnFrame;

    // 设置属性
    [_iconImgView setImageWithURL:[NSURL URLWithString:model.evaluate_icon] placeholderImage:DEFAULT_IMAGE_1_2];
    _usernameLabel.text             = model.evaluate_name;
    _contentLabel.text              = model.evaluate_content;
    _skuLabel.text                  = model.evaluate_sku;
   
    [self setupTagsView:model.evaluate_tags];
}

- (void)setupTagsView:(NSArray *)tags {
    
    [_tagView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat x, y, width, height;
    CGFloat margin = 10;
    CGFloat offset = margin;
    NSString *text;
    for (int i = 0; i < tags.count; i ++) {
        text = tags[i];
        width = [text getSizeOfString:14].width + 10;
        height = _tagView.height;
        x = offset;
        y = 0;
        if (offset + width >= _tagView.width - 10) {
            break;
        }

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        label.backgroundColor = rgba(254, 210, 193, 1);
        label.font = FONT(12);
        label.text = text;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.layer.cornerRadius = 5;
        label.layer.masksToBounds = YES;
        [_tagView addSubview:label];
        
        offset += (width + margin);
    }
}



#pragma mark - 点击事件
- (void)moreClick {
    
    if (self.lookMoreBlock) {
        self.lookMoreBlock();
    }
}
@end
